﻿import routeListIndex1 from '../../router/index-1.js';
import routeListIndex2 from '../../router/index-2.js';

let RoutesArray = [];
let perMenus = JSON.parse(window.localStorage.getItem("menus"));
// console.log('logs ', JSON.stringify(perMenus))

let perRoutes = [];
for (let i = 0; i < perMenus.length; i++) {
    let cur = perMenus[i];
    perRoutes.push({
        url: `/${cur.url}/`
    })
    for (let j = 0; j < cur.child.length; j++) {
        perRoutes.push({
            url: `/${cur.child[j].url}`
        })
    }
}
// console.log('perRoutes ',perRoutes)
for (let i = 0; i < perRoutes.length; i++) {
    RoutesArray.push(routeListIndex1.filter(value => value.path === perRoutes[i].url)[0])
}
RoutesArray.push({
    path: '*',
    redirect: RoutesArray[0].path

});

// RoutesArray=routeListIndex1
RoutesArray = RoutesArray.filter(value =>{
    if(!value){
                return false
            }
        return value.meta && !value.meta.isNotPermission
} 
);


RoutesArray = RoutesArray.filter((value) => {
    if(!value){
        return false
    }

 return value.path !== "/quoted-list/add" && value.path !== "/quoted-price/edits"
});
// console.log('RoutesArray ', RoutesArray)
import createUtil from '../../libs/js/util.js';

const Util = createUtil();

const state = {
    user: {
        uid: 0,
        nickname: 'none',
        portrait: require('../../../static/images/user-portrait.png'),
        token: '',
        authority: 1
    },
    navigation: {
        main: false, user: false,
        menus: []
    },
    datePickers: {
        shortcuts: [
            {
                text: '今天',
                onClick(picker) {
                    picker.$emit('pick', [new Date(), new Date()]);
                }
            },
            {
                text: '昨天',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24);
                    picker.$emit('pick', [start, end]);
                }
            },
            {
                text: '最近三天',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
                    picker.$emit('pick', [start, end]);
                }
            },
            {
                text: '最近七天',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                    picker.$emit('pick', [start, end]);
                }
            },
            {
                text: '最近六个月',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date();
                    start.setMonth(start.getMonth() - 6);
                    picker.$emit('pick', [start, end]);
                }
            },
            {
                text: '今年至今',
                onClick(picker) {
                    const end = new Date();
                    const start = new Date(new Date().getFullYear(), 0);
                    picker.$emit('pick', [start, end]);
                }
            }
        ]
    }
};

const getters = {
    getUserDataBySessionStorage() {
        var user = window.sessionStorage.getItem('user') || false;
        if (user && user != 'undefined') {
            user = JSON.parse(user);
        }
        return user;
    }
};

const mutations = {
    setElementValue(state, params) {
        // console.log(params);

        if (params instanceof Array) {
            for (let x of params) {
                let {key, value} = x,
                    k = key.split('.');

                switch (k.length) {
                    case 2:
                        state[k[0]][k[1]] = value;
                        break;
                    case 3:
                        state[k[0]][k[1]][k[2]] = value;
                        break;
                    default:
                        state[k[0]] = value;
                        break;
                }
            }
        } else {
            let {key, value} = params;
            state[key] = value;
        }
    },
    buildRouterDataset(state) {
        var keys = {}, children = [];

        for (let x of RoutesArray) {
            if (!x.meta) continue;
            /*   if (state.user.authority > 1) {
                   if (x.meta.authorize.indexOf(state.user.authority) < 0) continue;
               }*/
            // console.log('x.meta.menu.parent ', x)
            if (!x.meta.menu.parent) {
                state.navigation.menus.push({
                    index: x.name,
                    path: x.path,
                    name: x.meta.menu.name,
                    icon: x.meta.menu.icon,
                    children: []
                });
                // console.log(state.navigation.menus)
                keys[x.name] = state.navigation.menus.length - 1;
               
            } else {
                children.push(x);
                // console.log('children ',children)
            }
        }

        for (let x of children) {
            if (!x.meta) continue;
            /*   if (state.user.authority > 1) {
                   if (x.meta.authorize.indexOf(state.user.authority) < 0) continue;
               }
   */
            // console.log('logs ',)
            var index = keys[x.meta.menu.parent];
            // console.log('logs ', `${index}  ======   ${x.meta.menu.parent}`)
            if (index !== undefined) {
                state.navigation.menus[index].children.push({
                    index: x.name,
                    path: x.path,
                    name: x.meta.menu.name,
                    icon: x.meta.menu.icon,
                    image: x.meta.menu.image
                });
            }

        }
        // console.log('parent ', state.navigation.menus)

    },
    initializeUserDataset(state, params) {
        var {
            uid, nickname, portrait, token, authority
        } = params;

        state.user.uid = uid;
        state.user.nickname = nickname;
        state.user.token = token;

        if (authority) state.user.authority = authority;
        if (portrait) state.user.portrait = portrait;

        window.sessionStorage.setItem('user', JSON.stringify(state.user));
    }
};

const actions = {
    checkUserIdentity({state, getters, commit}) {
        var user = getters.getUserDataBySessionStorage;

        if (user) {
            commit('initializeUserDataset', user);
        } else {
            window.location.href = 'res/login/index.html';
        }

        var url = '',
            params = {};

        Util.postRequestInterface(url, params, (res) => {
            commit('initializeUserDataset', res.data);
        });
    }
};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
};
